package gw.com.android.ui.news;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.views.GuideView;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2017-12-11.
 */
public class FMGuideLayer {

    private static final FMGuideLayer instance = new FMGuideLayer();

    private FMGuideLayer() {
    }

    public static FMGuideLayer getInstance() {
        return instance;
    }

    private boolean showed = false;

    public void run(Activity activity, View anchor) {
        if (showed) {
            return;
        }
        if (activity == null || anchor == null) {
            return;
        }
        showed = GTConfig.instance().getBooleanValue("pref_fm_layer", false);
        if (showed) {
            return;
        }
        attach(activity, anchor, 1);
    }

    public void run(Fragment fragment, int id) {
        if (showed) {
            return;
        }
        if (fragment == null || fragment.getActivity() == null || id == 0) {
            return;
        }
        showed = GTConfig.instance().getBooleanValue("pref_fm_layer", false);
        if (showed) {
            return;
        }
        View anchor = fragment.getActivity().getWindow().getDecorView().findViewById(id);
        if (anchor == null) {
            return;
        }
        attach(fragment.getActivity(), anchor, 1);
    }

    private void attach(final Activity activity, final View anchor, final int count) {
        if (activity.isFinishing()) {
            return;
        }
        if (count > 2) {
            return;
        }
        if (activity instanceof MainActivity) {
            MainActivity main = (MainActivity) activity;
            if (ConfigType.TAB_HOME_TAG.equals(main.mCurrentTag) || ConfigType.TAB_ONLIVE_TAG.equals(main.mCurrentTag)) {
                return;
            }
        }
        int[] location = new int[2];
        anchor.getLocationInWindow(location);
        if (location[0] <= 0 && location[1] <= 0) {
            anchor.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    anchor.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    attach(activity, anchor, count + 1);
                }
            });
            return;
        }

        final GuideView layer = (GuideView) View.inflate(activity, R.layout.view_fm_guide, null);
        View mask = layer.findViewById(R.id.mask);
        int osmosis = DeviceUtil.instance().dip2px(2, activity);
        layer.setOsmosis(osmosis, osmosis, 0, osmosis);
        layer.addUncoverView(mask);
        layer.setCoverColor(0x88000000);
        final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mask.getLayoutParams();
        params.topMargin = location[1] - (params.height - anchor.getHeight()) / 2;
        mask.setLayoutParams(params);

        ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
        parent.addView(layer);
        layer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroup parent = (ViewGroup) v.getParent();
                parent.removeView(v);
            }
        });

        showed = true;
        GTConfig.instance().setBooleanValue("pref_fm_layer", true);
    }

}
